<?php
require('init.php');
$title = 'Next Contests';
require(LIBWWWDIR . '/header.php');
echo now();

echo "<h1> Next Contests</h1>\n\n";
//verificar de que tipo de usuario se trata
$userType = $userdata[18];
echo $userdata[0].$userdata[18];

if( $userType == '0' ){	//si el usuario es externo
	$res = $DB->q('TABLE SELECT * FROM contest WHERE post = 0 AND (categoryid = 3 OR categoryid = 4 )
			ORDER BY endtime DESC');
}elseif($userType == '1'){	//si el usuario es interno
	$res = $DB->q('TABLE SELECT * FROM contest WHERE post = 0 ORDER BY endtime DESC');
	//$res = $DB->q('TABLE SELECT * FROM contest WHERE DATE_SUB(activatetime,INTERVAL 30 MINUTE) ');
}


if( count($res) == 0 ) {
	echo "<p class=\"nodata\">No contests defined</p>\n\n";
} else {
	echo "<table class=\"list sortable\"><thead>\n" .
	     "<tr><th scope=\"col\">name</th><th scope=\"col\">active</th>".
	     "<th scope=\"col\">starts</th><th scope=\"col\">ends</th>" .
	     "</tr>\n</thead>\n<tbody>\n";

	$iseven = false;
	foreach($res as $row) {
//muestra los proximos contest justo antes del tiempo de activación, falta tener en cuenta tiempo de cierre
		if( difftime( now(),$row['endtime']) < 0  AND difftime( now(),$row['activatetime']) < 0  ){
			
		//$link = '<a href="contest_h.php?id=' . urlencode($row['cid']) . '">';

		echo '<tr class="' .
			( $iseven ? 'roweven': 'rowodd' ) .
			(!$row['enabled']    ? ' disabled' :'') .
			($row['cid'] == $cid ? ' highlight':'') . '">' .
			"<td align=\"right\">".htmlspecialchars($row['contestname'])."</td>\n".
			"<td title=".htmlspecialchars(@$row['activatetime']) .">" .
			printtime($row['activatetime'])."X".
				$date = $row['activatetime'];
				$fecha = new DateTime($date);
				$fecha->sub(new DateInterval('PT00H30M00S'));
				echo $fecha->format('Y-m-d H:i:s') . "Y".			
			"</td>".
			"<td title=".htmlspecialchars(@$row['starttime']) .">" .
			printtime($row['starttime'])."</td>".
			"<td title=".htmlspecialchars(@$row['endtime']) .">" .
			printtime($row['endtime'])."</td>";
		$iseven = ! $iseven;
		$cid = $row['cid'];
		$tc = $row['categoryid'];
		echo '<td class="editdel">';
		if( $tc == '3' ){
			echo '<a href="form3.php?c='.$cid.'">'.
		'<img src="../images/player_play.png" alt="edit" title="Inscribirme" class="picto"/></a>';
		}else{
			echo '<a href="habilitador.php?c='.$cid.'">'.
		'<img src="../images/player_play.png" alt="edit" title="Inscribirme" class="picto"/></a>';
		}
		echo '</td>\n';
	}
	}
	echo "</tbody>\n</table>\n\n";
}


echo "<a href=\"../team\"  target=\"_blank\">PARTICIPAR EN CONTEST</a>";













require(LIBWWWDIR . '/footer.php');
?>
